<?php

namespace app\admin\controller\admin;

use app\admin\controller\Controllers;
use app\admin\service\PermissionCategory as PermissionCategoryService;
use app\admin\model\admin\PermissionCategory as PermissionCategoryModel;
use app\exceptions\OrdinaryException;

class PermissionCategory extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(PermissionCategoryService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\PermissionCategory.index');
        $list = $service->indexPaginate($get);
        $config = config('field.permission_category');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'status' => $config['status']
        ]);
        return $this->fetch('admin/permission_category/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create( \app\admin\service\Permission $service )
    {
        $config = config('field.permission_category');
        $this->assign([
            'status' => $config['status']
        ]);
        return $this->fetch('admin/permission_category/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function save(PermissionCategoryService $service)
    {
        $post = $this->request->only(['name','description','status','url','path'], 'post');
        $this->validate($post, 'app\admin\validate\PermissionCategory.create');
        $post['create_time'] = time();
        $service->create($post);
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function edit(PermissionCategoryService $service, int $id)
    {
        $data = $service->get( $id );
        $config = config('field.permission_category');
        $this->assign([
            'data' => $data,
            'status' => $config['status']
        ]);

        return $this->fetch('admin/permission_category/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function update(PermissionCategoryService $service,int $id)
    {
        if ( $id <= 0 ) {
            return failed('服务器错误');
        }
        $put = $this->request->only(['name','description','status','url','path'], 'put');
        $put['id'] = $id;
        $this->validate($put, 'app\admin\validate\PermissionCategory.update');
        $service->create($put);
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function delete(PermissionCategoryService $service,int $id)
    {
        if ( $id <= 0 ) {
            return failed('参数错误,刷新重试!');
        }
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     * @author zx
     * @date 2019-11-14
     * @return \think\Response
     */
    public function status(PermissionCategoryService $service, PermissionCategoryModel $model)
    {
        return $service->status($model);
    }

    /**
     * 检测用户名是否唯一
     * @author zx
     * @date 2019-11-14
     * @return \think\Response
     */
    public function only(PermissionCategoryService $service)
    {
        $get = $this->request->only(['id','name'], 'get');
        $this->validate($get, 'app\admin\validate\PermissionCategory.only');
        $bool = $service->only($get);
        return json($bool);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-11-12
     * @throws OrdinaryException
     * @return \think\Response
     */
    public function destory( PermissionCategoryService $service )
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\PermissionCategory.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
